package com.seewintech.framework.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.seewintech.framework.common.http.Result;
import com.seewintech.framework.common.page.PageQuery;
import com.seewintech.framework.common.success.AjaxResult;
import com.seewintech.framework.entity.Account;
import com.seewintech.framework.mapper.AccountMapper;
import com.seewintech.framework.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author by xiaoTian
 * @date 2022/6/14.
 */
@Slf4j
@RestController
@RequestMapping("/admin/user")
public class AccountController {

    @Autowired
    AccountService accountService;
    @Autowired
    AccountMapper accountMapper;

    @GetMapping("/list")
    public Result<PageInfo<Account>> list(@RequestBody PageQuery vo){
        PageHelper.startPage(vo.getOffset(), vo.getLimit());
        List<Account> list = accountMapper.selectAccountList();
        PageInfo<Account> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    @GetMapping("/queryById/{id}")
    public Result<Account> query(@PathVariable("id") Long id){
        return Result.success(accountService.selectAccountById(id));
    }

    @PostMapping("/remove/{id}")
    public Result<AjaxResult> remove(@PathVariable("id") Long id){
        return Result.ajaxResult(new AjaxResult(accountService.remove(id)));
    }

    @PostMapping("/update")
    public Result<AjaxResult> update(@RequestBody Account account){
        return Result.ajaxResult(new AjaxResult(accountService.update(account)));
    }

    @PostMapping("/add")
    public Result<AjaxResult> add(@RequestBody Account account){
        return Result.ajaxResult(new AjaxResult(accountService.insert(account)));
    }

}
